﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SCG.SolarVengeanceEngine;
using SCG.General;

namespace SCG.SolarVengeance
{
    public partial class ChatPanel : BasePanel
    {
        //constructors
        public ChatPanel()
            : base()
        {
            InitializeComponent();
        }

        //make it snappy
        public override void Initialize()
        {
            Refresh();
            txtChat.Focus();
        }

        //selected outgoing filter
        public ChatChannel Channel { get; set; }

        //incoming chat
        public void GameChat(Player p, ChatChannel channel, string msg)
        {
            if (Game.CurrentPlayer == null)
                return;
            bool show = channel == ChatChannel.All;
            if (channel == ChatChannel.Friendly)
                show = Game.CurrentPlayer.FriendlyTo(p);
            else if (channel == ChatChannel.Enemy)
                show = !Game.CurrentPlayer.FriendlyTo(p);
            if (show)
                txtChatLog.Text += p.Name + ": " + msg + Environment.NewLine;
            Refresh();
            if (MainForm.GameCoordinator.IsHost)
                SendChatToBrains(p, channel, msg);
        }

        //enable send button?
        private void txtChat_TextChanged(object sender, EventArgs e)
        {
            btnSend.Enabled = txtChat.Text != "";
        }

        //send chat
        private void btnSend_Click(object sender, EventArgs e)
        {
            SendChat();
        }
        private void txtChat_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                SendChat();
        }
        private void SendChat()
        {
            string s = "GAMECHAT|" + ((int)Channel) + "|" + Game.CurrentPlayer.Name + "|" + txtChat.Text;
            txtChatLog.Text += Game.CurrentPlayer.Name + ": " + txtChat.Text + Environment.NewLine;
            if (MainForm.GameCoordinator.IsHost)
                SendChatToBrains(Game.CurrentPlayer, Channel, txtChat.Text);
            txtChat.Text = "";
            MainForm.Prism.SendData(s);
            Refresh();
        }

        //send chat to brains
        public void SendChatToBrains(Player p, ChatChannel channel, string msg)
        {
            foreach(Player brain in Game.Players)
                if (!brain.IsHuman)
                    if (!brain.Eliminated)
                    {
                        bool send = channel == ChatChannel.All;
                        if (!send)
                        {
                            if (channel == ChatChannel.Friendly)
                                send = p.AlliedWith(brain) != AllianceLevel.None;
                            else if (channel == ChatChannel.Enemy)
                                send = p.AlliedWith(brain) == AllianceLevel.None;
                        }
                        if (send)
                            brain.Brain.IncomingMessage(p.Name, msg);
                    }
        }

        //change outgoing channel
        private void btnAll_DownStateChanged(object sender, EventArgs e)
        {
            StateButton btn = sender as StateButton;
            if (btn.Down)
            {
                string s = btn.Tag as string;
                Channel = (ChatChannel)Int32.Parse(s);
            }
        }
    }
}
